#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "com_lmx_creceive_receive.h"
//can_receive_head
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>

//时间戳
#include <time.h>
//int num=0;
const int array_length = 8; 
/*void writefile(int data[array_length+1],struct tm *p)
{
    char* filename;
    filename=(char*)malloc(60*sizeof(char));
    ++num;
    snprintf(filename,55,"/root/device/home/pi/%d-%d-%d_data%d.txt",p->tm_year+1900,p->tm_mon+1,p->tm_mday,num);
    //int dataindex =ponitnumber;
    int* writedata =data;
    FILE *fp =fopen(filename,"w");
    for(int i=0;i<array_length;i++)
    fprintf(fp,"%d\n",writedata[i]);
    fclose(fp);

}*/
//can_receive
void can_receive(int *x)
{
    int ret;
    int s, nbytes;
    struct sockaddr_can addr;
    struct ifreq ifr;
    struct can_frame frame;
    
    memset(&frame, 0, sizeof(struct can_frame));
    
	 //system("sudo ip link set can0 type can bitrate 2500000");
 	 //system("sudo ifconfig can0 up");
system("sudo ip link set can0 up type can bitrate 250000");
    // printf("this is a can receive demo\r\n");
    
    //1.Create socket
    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (s < 0) {
        perror("socket PF_CAN failed");
        //return 1;
        return;
    }
    //2.Specify can0 device
    strcpy(ifr.ifr_name, "can0");
    ret = ioctl(s, SIOCGIFINDEX, &ifr);
    if (ret < 0) {
        perror("ioctl failed");
        //return 1;
        return;
    }

    //3.Bind the socket to can0
    addr.can_family = PF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
    ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) {
        perror("bind failed");
        //return 1;
        return;
    }
    //4.Define receive rules
    struct can_filter rfilter[1];
    rfilter[0].can_id = 0x123;
    rfilter[0].can_mask = CAN_SFF_MASK;
    setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
    //5.Receive data and exit
    //printf("1\n");
    int num=0;
    while(1) {
        nbytes = read(s, &frame, sizeof(frame));
        time_t timep;
//printf("3\n");
        if(nbytes > 0) {
            //printf("2\n");
            // printf("can_id = 0x%X\r\ncan_dlc = %d \r\n", frame.can_id, frame.can_dlc);
            // printf("%s",ctime(&timep));
            int i = 0;
            for(i = 0; i < 8; i++)
               {
               x[i]=frame.data[i];
            //    printf("data[%d] = %d\r\n", i, frame.data[i]);
               }
            //time (&timep);
            //struct tm *p;
            //p = gmtime(&timep);
            //writefile(x,p);
               break;
        }
    }
    //6.Close the socket and can0
    close(s);
    system("sudo ifconfig can0 down");
    //return 0;
}
JNIEXPORT jintArray JNICALL Java_com_lmx_creceive_receive_canreceive(JNIEnv *env,jobject obj){
	int i;
    int *can_data;
    int candata0[array_length];
    can_data =candata0;
    //char str[array_length+1];
    
        //system("sudo ip link set can0 up type can bitrate 250000");
        //system("sudo dhclient -v usb0");
        can_receive(can_data);
    jintArray newArr = (*env)->NewIntArray(env,array_length);
    (*env)->SetIntArrayRegion(env,newArr,0,array_length,can_data);
    //puts(str);
    return newArr;
}